package sc.tool.unionpay;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;

import sc.prototype.common.ConsoleUtil;

public class AreaCodeTool implements ConsoleUtil {

    public static final String sourceFilePath = "D:/WeibopaySVN/basis_doc/PPS/2_系统设计/支行信息/银联卡跨行业务地区代码标准（2010年8月版）.doc";

    public String getCellText(TableCell cell) {
        return cell.getParagraph(0).text().trim().replaceAll("　| ", "");
    }

    public void process() throws FileNotFoundException, IOException {
        HWPFDocument doc = new HWPFDocument(new FileInputStream(sourceFilePath));
        Range range = doc.getRange();
        TableIterator it = new TableIterator(range);
        it.hasNext();
        it.next();
        it.hasNext();
        Table table = it.next();
        TableRow row = null;
        int limitRow = 100000;
        //        TableCell cell = null;
        String areaName, areaCode, currentProv = null, currentCity = null;
        for (int i = 0; i != table.numRows(); i++) {
            row = table.getRow(i);
            if (row.numCells() == 2) {
                areaName = getCellText(row.getCell(0));
                areaCode = getCellText(row.getCell(1));
                if (StringUtils.isBlank(areaName) && StringUtils.isBlank(areaCode)) {
                    continue;
                }
                if (areaCode.matches("\\d{2}00")) {
                    currentProv = areaName;
                    currentCity = "";
                    if (areaName.matches(".*市")) {
                        currentCity = areaName;
                    }
                }
                if (areaCode.matches("\\d{2}[1-9]0")) {
                    currentCity = areaName;
                }
                System.out.println(String.format("%s\t%s\t%s\t%s", areaCode, areaName, currentCity, currentProv));
            }
            if (--limitRow <= 0) {
                break;
            }
        }
    }

    public void testShow(Range range) {
        TableIterator it = new TableIterator(range);
        int limitRow = 20;
        for (; it.hasNext();) {
            Table table = it.next();
            Console.info("numRows: {}", table.numRows());
            for (int i = 0; i != table.numRows(); i++) {
                TableRow row = table.getRow(i);
                for (int j = 0; j != row.numCells(); j++) {
                    TableCell cell = row.getCell(j);
                    for (int k = 0; k != cell.numParagraphs(); k++) {
                        Paragraph para = cell.getParagraph(k);
                        String text = para.text();
                        Console.info("Row:{}, Cell:{}, Paragraph:{}, Text: {}", new Object[] { i, j, k, text });

                    }
                }
                if (--limitRow <= 0) {
                    break;
                }
            }
            //            Console.info("ZZ:{}", table.getRow(0).getCell(0));
        }
    }

    public static void main(String[] args) {
        AreaCodeTool tool = new AreaCodeTool();
        try {
            tool.process();
        } catch (Exception e) {
            Console.error("", e);
        }
    }
}
